﻿namespace AppLab.Web
{
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Data.Objects.DataClasses;
    using System.IO;
    using System.Linq;
    using System.Net.Mail;
    using System.Web;
    using System.Web.Security;
    using AppLab.Data.Models;
    using AppLab.Web.Resources;
    using Wicresoft.AppFlow.Flows;
    using AppLab.Core;

    public partial class ReviewSubmitData : FormData
    {
        #region 表单升级包

        [Display(AutoGenerateField = false)]
        public bool UpgradePackageEnable { get; set; }

        /// <summary>
        /// 升级包
        /// </summary>
        string submitUpgradePackage;
        [Display(Order = 5, Name = "ReviewCustomDataFormSubmitUpgradePackageLable", ResourceType = typeof(RegistrationDataResources))]
        public string SubmitUpgradePackage { get { return submitUpgradePackage; } set { submitUpgradePackage = value; } }

        #endregion

        [Display(AutoGenerateField = false)]
        public FlowState ReviewResult { get; set; }

        [Display(AutoGenerateField = false)]
        public string ReviewComment { get; set; }

        [Display(AutoGenerateField = false)]
        public int Priority { get; set; }

        [Display(AutoGenerateField = false)]
        public DateTime TestTaskEAT { get; set; }

        [Display(AutoGenerateField = false)]
        public bool IsAccept { get; set; }

        public Guid ToDataBase()
        {
            try
            {
                using (var dc = PMSEntities.Create())
                {
                    var e = new tysx_Review();
                    e.ReviewResultID = (int)ReviewResult;
                    e.ReviewBy = ReviewBy;
                    e.ReviewTime = ReviewTime;
                    e.ReviewComment = ReviewComment;
                    dc.tysx_Review.AddObject(e);
                    dc.SaveChanges();

                    if (IsAccept)
                    {
                        var tysx_TestTask = new tysx_TestTask();
                        tysx_TestTask.AssignedTo = AssignedTo;
                        tysx_TestTask.MediaClientID = MediaClientID;
                        tysx_TestTask.Priority = Priority;
                        tysx_TestTask.StartDate = DateTime.Now;
                        tysx_TestTask.TestTaskETA = TestTaskEAT;
                        tysx_TestTask.RowGuid = this.RowGuid;
                        dc.tysx_TestTask.AddObject(tysx_TestTask);
                        e.TestTaskID = tysx_TestTask.TestTaskID;
                        dc.SaveChanges();
                    }
                    var flow = dc.tysx_AppFlow.First(p => p.FlowID == this.FlowGuid);
                    {
                        flow.StateID = e.ReviewResultID;
                        dc.SaveChanges();
                    }
                    var tysx_Submit = dc.tysx_Submit.First(p => p.SubmitID == flow.SubmitID);
                    {
                        tysx_Submit.ReviewID = e.ReviewID;
                        dc.SaveChanges();
                    }
                }
                return this.FlowGuid;
            }
            catch (Exception ex)
            {
                Log.WriteError(string.Format("{0}({1}): Reviewed is failed....", this.MoblieBrandAndType, this.MobileVersion));
                Log.WriteError(ex.Message);
                Log.WriteError(ex.StackTrace);
                return Guid.Empty;
            }
        }

        //public EntityObject ToDataBaseFormat()
        //{
        //    if (IsAccept)
        //    {
        //        return new tysx_Review()
        //        {
        //            ReviewResultID = (int)ReviewResult,
        //            ReviewBy = ReviewBy,
        //            ReviewTime = ReviewTime,
        //            ReviewComment = ReviewComment,

        //            tysx_TestTask = new tysx_TestTask()
        //            {
        //                AssignedTo = AssignedTo,
        //                MediaClientID = MediaClientID,
        //                Priority = Priority,
        //                StartDate = DateTime.Now,
        //                TestTaskETA = TestTaskEAT,
        //                RowGuid = this.RowGuid,
        //            },
        //        };
        //    }
        //    else
        //    {
        //        return new tysx_Review()
        //       {
        //           ReviewResultID = (int)ReviewResult,
        //           ReviewBy = ReviewBy,
        //           ReviewTime = ReviewTime,
        //           ReviewComment = ReviewComment,
        //       };
        //    }
        //}

        #region 发送审核Email

        public void SendEmail()
        {
            string toList = "";
            string cCList = "";

            using (var dc = PMSEntities.Create())
            {
                if (IsAccept) toList = dc.aspnet_Membership.FirstOrDefault(p => p.aspnet_Users.UserName == AssignedTo).Email;
                else toList = dc.aspnet_Membership.FirstOrDefault(p => p.aspnet_Users.UserName == SubmitByAlias).Email;

                cCList = dc.aspnet_Membership.FirstOrDefault(p => p.aspnet_Users.UserName == ReviewBy).Email;
            }

            if (string.IsNullOrEmpty(toList)) return;

            if (IsAccept)
            {
                //审核通过，告知验收者(user)
                string subject = "新的适配等待验收！";
                string bodyStr = System.IO.File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Resources\ReviewPackageAccepted.htm"));
                bodyStr = string.Format(bodyStr, this.AssignedTo, this.MobileBrand, this.MobileType, this.MobileVersion,
                    this.SubmitBy, this.SubmitTime.ToString("yyyy-MM-dd HH:mm"), this.ReviewBy, this.ReviewTime.ToString("yyyy-MM-dd HH:mm"),
                    this.TestTaskEAT.ToString("yyyy-MM-dd"), this.Priority);
                MailUtility.SendMail(toList, cCList, subject, bodyStr);
            }
            else
            {
                //审核失败，告知提交者(user)
                string subject = "适配安装包审核未通过！";
                string bodyStr = System.IO.File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Resources\ReviewRejectedPackage.htm"));
                bodyStr = string.Format(bodyStr, this.SubmitBy, this.MobileBrand, this.MobileType, this.MobileVersion,
                    this.SubmitTime.ToString("yyyy-MM-dd HH:mm"), this.ReviewBy, this.ReviewTime.ToString("yyyy-MM-dd HH:mm"), this.ReviewComment);
                MailUtility.SendMail(toList, cCList, subject, bodyStr);
            }

        #endregion
        }
    }
}